//
//  LFHomeViewController.m
//  LoveFreshBee
//
//  Created by 王忠迪 on 10/15/16.
//  Copyright © 2016 王忠迪. All rights reserved.
//

#import "LFHomeViewController.h"
#import "LFLocationView.h"
#import <Masonry.h>
#import <SDWebImageDownloader.h>
#import "LFNetworkTools.h"

@interface LFHomeViewController ()

@property (nonatomic ,copy) NSString *urlStr;

@property (nonatomic ,strong) UIImageView *adImage;

@end

@implementation LFHomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    LFLocationView * locationView = [[LFLocationView alloc]initWithFrame:CGRectMake(0, 0, 150, 20)];

    self.navigationItem.titleView = locationView;
    
    [self loadAD];
    
}

- (void)loadAD {
    
    _urlStr = [[NSUserDefaults standardUserDefaults] objectForKey:@"adStr"];
    
    if (!_urlStr.length) {
        [self getImageData];
        return;
    }
    
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject stringByAppendingPathComponent:_urlStr.lastPathComponent];
    
    NSData *data = [NSData dataWithContentsOfFile:path];
    UIImage *image = [UIImage imageWithData:data];
    
    if (!image) {
        [self getImageData];
        return;
    }
    
    self.adImage.image = image;
    
    [UIView animateWithDuration:1.5 delay:1 options:0 animations:^{
        _adImage.transform = CGAffineTransformMakeScale(1.5, 1.5);
        _adImage.alpha = 0;
    } completion:^(BOOL finished) {
        [_adImage removeFromSuperview];
    }];
    
}

- (void)getImageData {
    
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    
    [param setValue:@"7" forKey:@"call"];
    
    [[LFNetworkTools sharedTools] POST_RequestWithUrlString:@"ad.json.php" parmas:param successBloock:^(NSDictionary * responseObject) {
        
        NSDictionary *dict = responseObject[@"data"];
        _urlStr = dict[@"img_name"];
        [[NSUserDefaults standardUserDefaults] setObject:_urlStr forKey:@"adStr"];
        [[NSUserDefaults standardUserDefaults] synchronize];
        
        [[SDWebImageDownloader sharedDownloader]downloadImageWithURL:[NSURL URLWithString:_urlStr] options:SDWebImageDownloaderHighPriority progress:^(NSInteger receivedSize, NSInteger expectedSize) {
            
        } completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) {
            NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject stringByAppendingPathComponent:_urlStr.lastPathComponent];
            [data writeToFile:path atomically:YES];
        }];

        
    } failureBlock:^(NSError *error) {
        
    }];
    
    
    
}

- (UIImageView *)adImage {
    
    if (_adImage == nil) {
        _adImage = [[UIImageView alloc]init];
        _adImage.backgroundColor = [UIColor blackColor];
        [self.tabBarController.view addSubview:_adImage];
        [_adImage mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self.tabBarController.view);
        }];
        _adImage.alpha = 1;
    }
    return _adImage;
}



@end
